/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;

namespace OpenServers.Net.Common.FileSystem.Disk {
    /// <summary>
    /// directory backed by disk representing a directory on the disk.
    /// </summary>
    public class DiskDirectory : DiskFileSystemObject<DirectoryInfo>, IDirectory {
        /// <summary>
        /// constructor.
        /// </summary>
        /// <param name="parent">The parent directory</param>
        /// <param name="path">The full path to the directory.</param>
        public DiskDirectory(IDirectory parent, string path)
            : base(parent, new DirectoryInfo(path)) {
        }

        /// <summary>
        /// constructor.
        /// </summary>
        /// <param name="parent">The parent directory</param>
        /// <param name="directoryInfo">The directory info.</param>
        public DiskDirectory(IDirectory parent, DirectoryInfo directoryInfo)
            : base(parent, directoryInfo) {
        }

        /// <summary>
        /// Gets the subitems (files and directories).
        /// </summary>
        public IList<IFileSystemObject> GetItems() {
            List<IFileSystemObject> results = new List<IFileSystemObject>();
            if (Exists) {
                foreach (IDirectory dir in GetDirectories()) {
                    results.Add(dir);
                }
                foreach (FileInfo fileInfo in FileSystemInfo.GetFiles()) {
                    results.Add(new DiskFile(this, fileInfo));
                }
            }
            return results;
        }

        /// <summary>
        /// The list of directories only.
        /// </summary>
        public IList<IDirectory> GetDirectories() {
            IList<IDirectory> results = new List<IDirectory>();
            foreach (DirectoryInfo directoryInfo in FileSystemInfo.GetDirectories()) {
                results.Add(new DiskDirectory(this, directoryInfo));
            }
            return results;
        }

        /// <summary>
        /// Creates the directory.
        /// </summary>
        public void Create() {
            FileSystemInfo.Create();
        }

        /// <summary>
        /// Creates a file, in the directory.
        /// </summary>
        public virtual IFile CreateFile(string name) {
            return new DiskFile(this, FileSystemInfo.FullName + "\\" + name);
        }

        /// <summary>
        /// Creates a sub directory.
        /// </summary>
        public IDirectory CreateDirectory(string name) {
            return new DiskDirectory(this, FileSystemInfo.FullName + "\\" + name);
        }

        /// <summary>
        /// moves the object to another location.
        /// </summary>
        public override void MoveTo(IFileSystemObject fso) {
            if (fso is IDirectory) {
                IDirectory newDir = (IDirectory)fso;
                IList<IFileSystemObject> children = GetItems();
                foreach (IFileSystemObject child in children) {
                    if (child is IFile) {
                        IFile newFile = newDir.CreateFile(child.Name);
                        child.MoveTo(newFile);
                    } else {
                        IDirectory d = newDir.CreateDirectory(child.Name);
                        child.MoveTo(d);
                    }
                }
            } else {
                throw new ArgumentException("target must be a directory");
            }
        }

        /// <summary>
        /// Gets an object from the directory.
        /// </summary>
        /// <param name="path">The path is seperated by '/'</param>
        public IFileSystemObject GetObject(string path) {
            return FileSystemUtils.GetObject(this, path);
        }
    }
}
